﻿/**
 * Create in 2012-4-5.
 */
package kit4j.common.event;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 
 * @author MiniKnife
 *
 */
public abstract class AbstractConcurrentEventFirer<L, E> extends AbstractEventFirer<L, E> {
	
	private ExecutorService executor;
	
	public AbstractConcurrentEventFirer() {
		executor = Executors.newCachedThreadPool();
	}

	/**
	 * @see kit4j.common.event.EventFirer#fireEvent(java.lang.Object)
	 */
	@Override
	public void fireEvent(final E evt) {
		executor.submit(new Runnable() {

			@Override
			public void run() {
				L[] listeners = getListeners();
				for(L listener : listeners) {
					fireEventToListener(evt, listener);
				}
			}
			
		});
	}
	
	abstract protected void fireEventToListener(E evt, L listener);

}
